package cn.hello1023.todolist.controller;

import cn.hello1023.todolist.Result;
import cn.hello1023.todolist.model.Todo;
import cn.hello1023.todolist.service.TodoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class Demo {
    @Autowired
    TodoService todoService;
    @GetMapping("/")
    public String hello(){
        return "Hello World";
    }
    @GetMapping("/getTodoList")
    public Result<List<Todo>>  getTodolist() {
        Result<List<Todo>> result = new Result<>();

        List<Todo> list = todoService.getTodoList();
        result.setCode(200);
        result.setMsg("操作成功");
        result.setData(list);
        return result;
    }

    @PostMapping("/addTodo")
    public String addTodo(@RequestParam(name = "title") String title){
        System.out.println(title);
        // 接收到参数后，将数据插入到数据库
        todoService.addTodo(title);
        return "OK!";
    }
}
